package org.linlinjava.litemall.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.dto.AdminFormulaMaterialReplaceableMaterialDTO;
import org.linlinjava.litemall.admin.dto.AdminLumiereFormulaSpecificationsDataProportioningDTO;
import org.linlinjava.litemall.admin.dto.AdminLumiereReplaceableMaterialDTO;
import org.linlinjava.litemall.admin.vo.AdminLumiereReplaceableMaterialVO;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @auth :ply
 * @date 2024/5/24 11:10
 * @Description TODO
 * 管理可替换原料服务
 */
@Service
@RequiredArgsConstructor
public class AdminLumiereReplaceableMaterialService {

    private final LumiereReplaceableMaterialService lumiereReplaceableMaterialService;

    private final LumiereCustomizationFormulaService lumiereCustomizationFormulaService;

    private final LumiereFormulaSpecificationsDataProportioningService lumiereFormulaSpecificationsDataProportioningService;

    private final LumiereFormulaMaterialService lumiereFormulaMaterialService;

    private final LumiereFormulaSpecificationsService lumiereFormulaSpecificationsService;

    private final LumiereGoodsFormulaCorrelationService lumiereGoodsFormulaCorrelationService;

    /**
     * 根据查询条件查询可替换原料列表
     * @param lumiereCustomizationFormula 查询条件
     * @return 配方类型列表
     */
    public ResponseUtil.Response listReplaceableMaterial(LumiereCustomizationFormula lumiereCustomizationFormula) {

        List<LumiereCustomizationFormula> materialList = lumiereCustomizationFormulaService.list(new LambdaQueryWrapper<LumiereCustomizationFormula>()
                        .eq(LumiereCustomizationFormula::getGoodsId,lumiereCustomizationFormula.getGoodsId())
                        .eq(LumiereCustomizationFormula::getFormulaId,lumiereCustomizationFormula.getFormulaId())
                        .eq(LumiereCustomizationFormula::getSpecificationsId,lumiereCustomizationFormula.getSpecificationsId()));

        List<AdminLumiereReplaceableMaterialDTO> replaceableMaterialDTOList = new ArrayList<>();
        materialList.forEach(material -> {
            AdminLumiereReplaceableMaterialDTO adminLumiereReplaceableMaterialDTO = new AdminLumiereReplaceableMaterialDTO();
            List<LumiereReplaceableMaterial> lumiereReplaceableMaterials = lumiereReplaceableMaterialService.list(new LambdaQueryWrapper<LumiereReplaceableMaterial>().eq(LumiereReplaceableMaterial::getId, material.getReplaceableId()));
            adminLumiereReplaceableMaterialDTO.setMaterialId(material.getMaterialId());
            adminLumiereReplaceableMaterialDTO.setReplaceableMaterialList(lumiereReplaceableMaterials);
            replaceableMaterialDTOList.add(adminLumiereReplaceableMaterialDTO);
        });

        // 返回结果
        return ResponseUtil.okPageInfo(replaceableMaterialDTOList);
    }

    /**
     * 根据查询条件查询可替换原料列表
     * @param lumiereCustomizationFormula 查询条件
     * @return 配方类型列表
     */
    public ResponseUtil.Response listFormulaMaterialReplaceableMaterial(LumiereCustomizationFormula lumiereCustomizationFormula) {

        //查询到规格的数据
        LumiereFormulaSpecifications specifications = lumiereFormulaSpecificationsService.getById(lumiereCustomizationFormula.getSpecificationsId());

        // 使用 Stream API 查询并处理数据
        List<AdminLumiereFormulaSpecificationsDataProportioningDTO> proportioningDTOList = lumiereFormulaMaterialService.list(
                        // 构造查询条件
                        new LambdaQueryWrapper<LumiereFormulaMaterial>()
                                .eq(LumiereFormulaMaterial::getFormulaId, specifications.getFormulaId())
                ).stream() // 将查询结果转换为 Stream
                .map(lumiereFormulaMaterial -> { // 遍历每个原料数据
                    // 创建 DTO 对象
                    AdminLumiereFormulaSpecificationsDataProportioningDTO formulaSpecificationsDataProportioningDTO = new AdminLumiereFormulaSpecificationsDataProportioningDTO();

                    //查询商品配方关联
                    LumiereGoodsFormulaCorrelation goodsFormulaCorrelation = lumiereGoodsFormulaCorrelationService.getGoodsFormulaByGoodsId(lumiereCustomizationFormula.getGoodsId());

                    //查询原料的定制表
                    List<LumiereCustomizationFormula> materialList = lumiereCustomizationFormulaService.list(new LambdaQueryWrapper<LumiereCustomizationFormula>()
                            .eq(LumiereCustomizationFormula::getGoodsId,lumiereCustomizationFormula.getGoodsId())
                            .eq(LumiereCustomizationFormula::getFormulaId,goodsFormulaCorrelation.getFormulaId())
                            .eq(LumiereCustomizationFormula::getSpecificationsId,lumiereCustomizationFormula.getSpecificationsId())
                            .eq(LumiereCustomizationFormula::getMaterialId,lumiereFormulaMaterial.getId()));

                    //查询原料定制匹配的可替换原料数据
                    List<LumiereReplaceableMaterial> replaceableMaterials = materialList.stream()
                            .map(material -> lumiereReplaceableMaterialService.getById(material.getId()))
                            .collect(Collectors.toList());

                    //封装数据
                    AdminFormulaMaterialReplaceableMaterialDTO adminFormulaMaterialReplaceableMaterialDTO = new AdminFormulaMaterialReplaceableMaterialDTO();
                    BeanUtils.copyProperties(lumiereFormulaMaterial,adminFormulaMaterialReplaceableMaterialDTO);
                    adminFormulaMaterialReplaceableMaterialDTO.setReplaceableMaterialList(replaceableMaterials);
                    formulaSpecificationsDataProportioningDTO.setAdminFormulaMaterialReplaceableMaterialDTO(adminFormulaMaterialReplaceableMaterialDTO);
                    // 设置配方原料数据
                    // formulaSpecificationsDataProportioningDTO.setLumiereFormulaMaterial(lumiereFormulaMaterial);

                    // 查询配方原料配比数据
                    LumiereFormulaSpecificationsDataProportioning proportioning = lumiereFormulaSpecificationsDataProportioningService.getOne(
                            new LambdaQueryWrapper<LumiereFormulaSpecificationsDataProportioning>()
                                    .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaMaterialId, lumiereFormulaMaterial.getId())
                                    .eq(LumiereFormulaSpecificationsDataProportioning::getFormulaSpecificationsId,lumiereCustomizationFormula.getSpecificationsId())
                    );

                    // 如果查询结果不为空，则复制属性
                    if (proportioning != null) {
                        BeanUtils.copyProperties(proportioning, formulaSpecificationsDataProportioningDTO);
                    }

                    return formulaSpecificationsDataProportioningDTO; // 返回 DTO 对象
                }).collect(Collectors.toList()); // 将结果收集为 List

        //返回结果
        return ResponseUtil.okPageInfo(proportioningDTOList);
    }

    /**
     * 添加可替换原料
     * @param adminLumiereReplaceableMaterialVO 待添加的可替换原料对象
     * @return 添加操作的响应结果
     */
    public ResponseUtil.Response addReplaceableMaterial(AdminLumiereReplaceableMaterialVO adminLumiereReplaceableMaterialVO) {
        boolean save = lumiereReplaceableMaterialService.save(adminLumiereReplaceableMaterialVO.getLumiereReplaceableMaterial());
        if(save){
            adminLumiereReplaceableMaterialVO.getLumiereCustomizationFormula().setReplaceableId(adminLumiereReplaceableMaterialVO.getLumiereReplaceableMaterial().getId());
            return lumiereCustomizationFormulaService.save(adminLumiereReplaceableMaterialVO.getLumiereCustomizationFormula()) ? ResponseUtil.ok(adminLumiereReplaceableMaterialVO.getLumiereCustomizationFormula().getId()) : ResponseUtil.fail();
        }else{
            return ResponseUtil.fail();
        }
    }

    /**
     * 更新可替换原料
     * @param lumiereReplaceableMaterial 待更新的可替换原料对象
     * @return 更新操作的响应结果
     */
    public ResponseUtil.Response updateReplaceableMaterial(LumiereReplaceableMaterial lumiereReplaceableMaterial) {
        return lumiereReplaceableMaterialService.updateById(lumiereReplaceableMaterial) ? ResponseUtil.ok(lumiereReplaceableMaterial.getId()) : ResponseUtil.fail();
    }

    /**
     * 根据指定ID删除可替换原料。
     * @param id 待删除的可替换原料ID
     * @return 删除操作的响应结果
     */
    public ResponseUtil.Response deleteReplaceableMaterial(Integer id){
        // 先删除关联表中的数据
        boolean removeCustomFormula = lumiereCustomizationFormulaService.remove(
                new LambdaUpdateWrapper<LumiereCustomizationFormula>().eq(LumiereCustomizationFormula::getReplaceableId, id)
        );
        if (removeCustomFormula) {
            // 然后删除主表中的数据
            return lumiereReplaceableMaterialService.removeById(id) ? ResponseUtil.ok() : ResponseUtil.fail();
        } else {
            return ResponseUtil.fail();
        }
    }

    /**
     * 是否定制
     * @param lumiereGoodsFormulaCorrelation 商品配方关联对象
     * @return 返回是否定制结果
     */
    public ResponseUtil.Response isCustomization(LumiereGoodsFormulaCorrelation lumiereGoodsFormulaCorrelation) {
        //查询商品配方关联
        LumiereGoodsFormulaCorrelation goodsFormulaCorrelation = lumiereGoodsFormulaCorrelationService.getOne(new LambdaQueryWrapper<LumiereGoodsFormulaCorrelation>().eq(LumiereGoodsFormulaCorrelation::getGoodsId, lumiereGoodsFormulaCorrelation.getGoodsId()));
        if(!ObjectUtils.isEmpty(goodsFormulaCorrelation)){
            return ResponseUtil.ok(goodsFormulaCorrelation);
        }else{
            return ResponseUtil.fail();
        }
    }

    /**
     * 修改是否定制
     * @param lumiereGoodsFormulaCorrelation 商品配方关联对象
     * @return 修改操作的响应结果
     */
    public ResponseUtil.Response updateIsCustomization(LumiereGoodsFormulaCorrelation lumiereGoodsFormulaCorrelation) {
        LambdaUpdateWrapper<LumiereGoodsFormulaCorrelation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(LumiereGoodsFormulaCorrelation::getIsCustomization, lumiereGoodsFormulaCorrelation.getIsCustomization())
                .eq(LumiereGoodsFormulaCorrelation::getGoodsId, lumiereGoodsFormulaCorrelation.getGoodsId());
        return lumiereGoodsFormulaCorrelationService.update(null, updateWrapper) ? ResponseUtil.ok() : ResponseUtil.fail();
    }


}
